package com.yang.frost.raspberry.shiro.jwt;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.yang.frost.raspberry.util.IpUtil;
import com.yang.frost.raspberry.util.JwtUtil;
import org.apache.shiro.authc.HostAuthenticationToken;

import java.util.Date;

/**
 * @author 10296
 * @date 2020/12/24
 * @Description:
 */
public class JwtToken implements HostAuthenticationToken {
    /**登录IP*/
    private String host;
    /**登录用户名称*/
    private String username;
    /**登录盐值*/
    private String salt;
    /**登录token*/
    private String token;
    /**创建时间*/
    private Date createDate;
    /**多长时间过期，默认一小时*/
    private Long expireSecond;
    /**过期日期*/
    private Date expireDate;

    private String principal;

    private String credentials;



    @Override
    public String getHost() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return token;
    }

    @Override
    public Object getCredentials() {
        return token;
    }

    private void setHost(String host) {
        this.host = host;
    }

    public String getUsername() {
        return username;
    }

    private void setUsername(String username) {
        this.username = username;
    }

    public String getSalt() {
        return salt;
    }

    private void setSalt(String salt) {
        this.salt = salt;
    }

    public String getToken() {
        return token;
    }

    private void setToken(String token) {
        this.token = token;
    }

    public Date getCreateDate() {
        return createDate;
    }

    private void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Long getExpireSecond() {
        return expireSecond;
    }

    private void setExpireSecond(Long expireSecond) {
        this.expireSecond = expireSecond;
    }

    public Date getExpireDate() {
        return expireDate;
    }

    private void setExpireDate(Date expireDate) {
        this.expireDate = expireDate;
    }

    public void setPrincipal(String principal) {
        this.principal = principal;
    }

    public void setCredentials(String credentials) {
        this.credentials = credentials;
    }

    public static JwtToken build(String token, String username, String salt,
                                 long expireSecond) {
        DecodedJWT decodedJwt = JwtUtil.getJwtInfo(token);
        Date createDate = decodedJwt.getIssuedAt();
        Date expireDate = decodedJwt.getExpiresAt();
        JwtToken jwtToken = new JwtToken();
        jwtToken.setToken(token);
        jwtToken.setUsername(username);
        jwtToken.setHost(IpUtil.getRequestIp());
        jwtToken.setSalt(salt);
        jwtToken.setCreateDate(createDate);
        jwtToken.setExpireSecond(expireSecond);
        jwtToken.setExpireDate(expireDate);
        return jwtToken;
    }


}
